﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;

namespace YJKArch.GlobleSetModule
{
	public partial class DlgMultModify : System.Windows.Forms.Form
	{
		public DlgMultModify()
		{
			this.InitializeComponent();
		}

		public DlgMultModify(UIApplication app, ref List<LevelsDataSource> srcData)
		{
			this.m_application = app;
			this.m_docment = app.ActiveUIDocument.Document;
			this.m_srcData = srcData;
			this.InitializeComponent();
			this.InitLevelType();
			this.UpdatePreviewItem();
		}

		public void InitLevelType()
		{
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_docment, typeof(LevelType), BuiltInCategory.OST_Levels))
			{
				string name = (element as LevelType).Name;
				if (!this.cmbSymType.Items.Contains(name))
				{
					this.cmbSymType.Items.Add(name);
				}
			}
			if (this.cmbSymType.Items.Count > 0)
			{
				this.cmbSymType.SelectedIndex = 0;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			int num = 0;
			if (!(this.tbStartNum.Text != ""))
			{
				MessageBox.Show("起始编号不能为空！", "盈建科提示");
				this.tbStartNum.Focus();
				return;
			}
			if (!int.TryParse(this.tbStartNum.Text, out num))
			{
				MessageBox.Show("起始编号只能是整数！", "盈建科提示");
				this.tbStartNum.Focus();
				return;
			}
			if (!Assist.StringUtil.isNumberic2(this.tbDefaultH.Text))
			{
				MessageBox.Show("楼层高度只能是正实数！", "盈建科提示");
				this.tbDefaultH.Focus();
				return;
			}
			if (this.rbAdd.Checked)
			{
				this.type = 0;
				int num2 = num + this.m_srcData.Count - 1;
				bool flag = Convert.ToInt32(this.tbStartNum.Text) < 0 && this.m_srcData.Count + Convert.ToInt32(this.tbStartNum.Text) > 0;
				for (int i = 0; i < this.m_srcData.Count; i++)
				{
					int num3;
					if (flag)
					{
						num3 = num2 - i;
						if (num3 >= 0)
						{
							num3++;
						}
					}
					else
					{
						num3 = num2 - i;
					}
					string text = num3.ToString();
					if (this.cbIndexUseCN.Checked)
					{
						text = ArabicNumeralsConvertChineseDigitalKit.NumToChn(text);
					}
					string name = this.tbBefore.Text + text + this.tbAfter.Text;
					if (this.cbModifyName.Checked)
					{
						this.m_srcData.ElementAt(i).Name = name;
					}
					if (this.cbModifyHeight.Checked)
					{
						this.m_srcData.ElementAt(i).LevelHeight = Convert.ToDouble(this.tbDefaultH.Text);
					}
					if (this.cbModifyType.Checked)
					{
						this.m_srcData.ElementAt(i).Type = this.cmbSymType.SelectedItem.ToString();
					}
				}
			}
			else if (this.rbCut.Checked)
			{
				this.type = 1;
				int num4 = num;
				bool flag2 = Convert.ToInt32(this.tbStartNum.Text) > 0 && this.m_srcData.Count - Convert.ToInt32(this.tbStartNum.Text) > 0;
				for (int j = 0; j < this.m_srcData.Count; j++)
				{
					int num5;
					if (flag2)
					{
						num5 = num4 - j;
						if (num5 <= 0)
						{
							num5--;
						}
					}
					else
					{
						num5 = num4 - j;
					}
					string text2 = num5.ToString();
					if (this.cbIndexUseCN.Checked)
					{
						text2 = ArabicNumeralsConvertChineseDigitalKit.NumToChn(text2);
					}
					string name2 = this.tbBefore.Text + text2 + this.tbAfter.Text;
					if (this.cbModifyName.Checked)
					{
						this.m_srcData.ElementAt(j).Name = name2;
					}
					if (this.cbModifyHeight.Checked)
					{
						this.m_srcData.ElementAt(j).LevelHeight = Convert.ToDouble(this.tbDefaultH.Text);
					}
					if (this.cbModifyType.Checked)
					{
						this.m_srcData.ElementAt(j).Type = this.cmbSymType.SelectedItem.ToString();
					}
				}
			}
			base.DialogResult = DialogResult.OK;
		}

		private void tbBefore_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbStartNum_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbAfter_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void cbModifyName_CheckedChanged(object sender, EventArgs e)
		{
			this.tbBefore.Enabled = this.cbModifyName.Checked;
			this.tbStartNum.Enabled = this.cbModifyName.Checked;
			this.tbAfter.Enabled = this.cbModifyName.Checked;
			this.rbAdd.Enabled = this.cbModifyName.Checked;
			this.rbCut.Enabled = this.cbModifyName.Checked;
			this.cbIndexUseCN.Enabled = this.cbModifyName.Checked;
		}

		private void cbModifyHeight_CheckedChanged(object sender, EventArgs e)
		{
			this.tbDefaultH.Enabled = this.cbModifyHeight.Checked;
		}

		private void cbModifyType_CheckedChanged(object sender, EventArgs e)
		{
			this.cmbSymType.Enabled = this.cbModifyType.Checked;
		}

		private void cbIndexUseCN_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbStartNum_Enter(object sender, EventArgs e)
		{
			this.tbStartNum.SelectAll();
		}

		private void tbStartNum_Click(object sender, EventArgs e)
		{
			this.tbStartNum.SelectAll();
		}

		private void UpdatePreviewItem()
		{
			try
			{
				string str = this.tbStartNum.Text;
				if (this.cbIndexUseCN.Checked)
				{
					str = ArabicNumeralsConvertChineseDigitalKit.NumToChn(this.tbStartNum.Text);
				}
				this.lblPreview.Text = "层名预览：" + this.tbBefore.Text + str + this.tbAfter.Text;
			}
			catch
			{
				this.lblPreview.Text = "层名预览：" + this.tbBefore.Text + this.tbAfter.Text;
			}
		}

		protected static int CharToNumber(char c)
		{
			if (c <= '二')
			{
				if (c <= '七')
				{
					if (c == '一')
					{
						return 1;
					}
					if (c == '七')
					{
						return 7;
					}
				}
				else
				{
					if (c == '三')
					{
						return 3;
					}
					if (c == '九')
					{
						return 9;
					}
					if (c == '二')
					{
						return 2;
					}
				}
			}
			else if (c <= '八')
			{
				if (c == '五')
				{
					return 5;
				}
				if (c == '八')
				{
					return 8;
				}
			}
			else
			{
				if (c == '六')
				{
					return 6;
				}
				if (c == '四')
				{
					return 4;
				}
				if (c == '零')
				{
					return 0;
				}
			}
			return -1;
		}

		protected static int CharToUnit(char c)
		{
			if (c <= '亿')
			{
				if (c == '万')
				{
					return 10000;
				}
				if (c == '亿')
				{
					return 100000000;
				}
			}
			else
			{
				if (c == '十')
				{
					return 10;
				}
				if (c == '千')
				{
					return 1000;
				}
				if (c == '百')
				{
					return 100;
				}
			}
			return 1;
		}

		public static int ParseCnToInt(string cnum)
		{
			cnum = Regex.Replace(cnum, "\\s+", "");
			int num = 1;
			int num2 = 1;
			int num3 = 1;
			int num4 = 0;
			for (int i = cnum.Length - 1; i > -1; i--)
			{
				num3 = DlgMultModify.CharToUnit(cnum[i]);
				if (num3 > num)
				{
					num = num3;
					num2 = 1;
				}
				else if (num3 > num2)
				{
					num2 = num3;
				}
				else
				{
					num4 += num * num2 * DlgMultModify.CharToNumber(cnum[i]);
				}
			}
			if (num3 == 10)
			{
				num4 += num * num3;
			}
			return num4;
		}

		private UIApplication m_application;

		private Document m_docment;

		private List<LevelsDataSource> m_srcData;

		public int type;
	}
}
